<?php
namespace app\backman\controller;
use app\backman\model\Meal;
use \app\common\controller\AuthBack;
use app\common\model\Ipconfig;
use app\common\model\Region;
use app\common\model\Scurl;
use lib\CashStatus;
use think\Db;
use think\facade\Env;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
use Intervention\Image;
use GuzzleHttp;
use think\facade\App;
require_once App::getRootPath().'/extend/PHPExcel/PHPExcel.php';


class Staff extends AuthBack{



    public function initialize(){
        parent::initialize();
    }

    public function index(){
        $model = new Scurl;
        $limit = isset($_GET['limit']) ? $_GET['limit'] : '20';
        
        $where = [];
        if( !empty(input('title')) ){
            $where[] = ['tg_url|tz_url', 'like', "%".input('title')."%"];
        }
        
//        if( !empty(input('tz_url'))){
//            $where[] = ['tz_url','eq',input('tz_url')];
//        }
        $totalNum = $model->count();
        if(request()->isAjax()){
            
            $page = isset($_GET['page']) ? $_GET['page'] : '1';
            $limit = isset($_GET['limit']) ? $_GET['limit'] : '20';
            $startNum = ($page - 1) * $limit;
            $totalPage = ceil($totalNum/$limit);
            $list = $model->where($where)->limit($startNum,$limit)->select();
            if(empty($list)){
                return json(['code'=>0,'data'=>[],'total'=>$totalPage,'count'=>$totalNum]);
            }elseif($page > $totalPage){
                return json(['code'=>0,'data'=>[],'total'=>$totalPage+1,'count'=>$totalNum]);
            }else{
                $nList = array();
                if(!empty($list)){
                    foreach($list as $k=>$vo){
                        $vo['kill_addr'] = implode(',',json_decode(html_entity_decode($vo['kill_addr']), true));
                        $vo['is_mobile'] = $vo['is_mobile'] ? '是' : '否';
                        $nList[$k] = $vo;
                        $nList[$k]['op'] = url('option',['id'=>$vo['id']]);
                    }
                }
                $return = [
                    'code'=>0,
                    'msg'=>'',
                    'count'=>$totalNum,
                    'data'=>$nList
                ];
                return json($return);
            }
        }else{
            $this->assign('totalNum',$totalNum);
            return view();
        }
    }

    

   

    public function del(){
        $res = DB::name('scurl')->where('id',$this->data['id'])->delete();
        if( $res ){
            return $this->success('操作成功',url('index'));
        }else{
            return $this->error('删除失败');
        }

    }

    public function status(){
        $status = DB::name('scurl')->where('id',$this->data['id'])->value('jump');
        $status = $status == 1 ? 0 : 1;
        $res = DB::name('scurl')->where('id',$this->data['id'])->update(['jump'=>$status]);
        if( $res ){
            return $this->success('操作成功',url('index'));
        }else{
            return $this->error('操作失败');
        }

    }

    /**
     * 编辑域名信息
     * @param int $id
     * @return \think\response\View|void
     */
    public function option($id = 0){
        if(!request()->isPost()){
            
            $info = Scurl::get($id);
            $province = Region::where('pid',0)->field('shortname')->select();
            $pro = [];

            foreach( $province as $item ){
                //{"value": "1", "title": "李白"}
                $pro[] = ['value'=>$item['shortname'],'title'=>$item['shortname']];
            }
            $this->assign('info',$info);
            $this->assign('pro',json_encode($pro));
            return view();
        }else{

            $obj = new Scurl();
            if($this->data['id']){
                // 编辑
                $state = $obj->saveData($this->data,'edit');
            }else{
                // 新增
                $state = $obj->saveData($this->data);
            }
            if($state){
                return $this->success('操作成功',url('index'));
            }else{
                return $this->error($obj->getError());
            }
        }
    }

    public function category(){
        $model = new Ipconfig();
        $limit = isset($_GET['limit']) ? $_GET['limit'] : '20';

        $where = [];
        if( !empty(input('title')) ){
            $where[] = ['ip', 'like', "%".input('title')."%"];
        }

//        if( !empty(input('tz_url'))){
//            $where[] = ['tz_url','eq',input('tz_url')];
//        }
        $totalNum = $model->count();
        if(request()->isAjax()){

            $page = isset($_GET['page']) ? $_GET['page'] : '1';
            $limit = isset($_GET['limit']) ? $_GET['limit'] : '20';
            $startNum = ($page - 1) * $limit;
            $totalPage = ceil($totalNum/$limit);
            $list = $model->where($where)->limit($startNum,$limit)->select();
            if(empty($list)){
                return json(['code'=>0,'data'=>[],'total'=>$totalPage,'count'=>$totalNum]);
            }elseif($page > $totalPage){
                return json(['code'=>0,'data'=>[],'total'=>$totalPage+1,'count'=>$totalNum]);
            }else{
                $nList = array();
                if(!empty($list)){
                    foreach($list as $k=>$vo){
                        $vo['status'] = $vo['status'] ? '是' : '否';
                        $nList[$k] = $vo;
                        $nList[$k]['op'] = url('category_op',['id'=>$vo['id']]);
                    }
                }
                $return = [
                    'code'=>0,
                    'msg'=>'',
                    'count'=>$totalNum,
                    'data'=>$nList
                ];
                return json($return);
            }
        }else{
            $this->assign('totalNum',$totalNum);
            return view();
        }
    }


    /**
     * 编辑ip信息
     * @param int $id
     * @return \think\response\View|void
     */
    public function category_op($id = 0){
        if(!request()->isPost()){

            $info = Ipconfig::get($id);
            $this->assign('info',$info);
            return view();
        }else{

            $obj = new Ipconfig();
            if($this->data['id']){
                // 编辑
                $state = $obj->saveData($this->data,'edit');
            }else{
                // 新增
                $state = $obj->saveData($this->data);
            }
            if($state){
                return $this->success('操作成功',url('category'));
            }else{
                return $this->error($obj->getError());
            }
        }
    }


}